package se.sics.drsm.model;

import java.io.*;
import java.util.Properties;

/**
 * The <code>SONConfiguration</code> class.
 *
 * @author Shadid Chowdhury <msrch@kth.se>
 * @version $Id: ChordConfiguration.java 1149 2009-09-01 23:55:47Z Cosmin $
 */
public final class SONConfiguration {

    private final int log2RingSize;
    private final int successorListLength;
    private final long successorStabilizationPeriod;
    private final long fingerStabilizationPeriod;
    private final long rpcTimeout;
    private final int bootstrapRequestPeerCount;

    public SONConfiguration(int log2RingSize, int successorListLength,
                            long successorStabilizationPeriod, long fingerStabilizationPeriod,
                            long rpcTimeout, int bootstrapRequestPeerCount) {
        super();
        this.log2RingSize = log2RingSize;
        this.successorListLength = successorListLength;
        this.successorStabilizationPeriod = successorStabilizationPeriod;
        this.fingerStabilizationPeriod = fingerStabilizationPeriod;
        this.rpcTimeout = rpcTimeout;
        this.bootstrapRequestPeerCount = bootstrapRequestPeerCount;
    }

    public int getLog2RingSize() {
        return log2RingSize;
    }

    public int getSuccessorListLength() {
        return successorListLength;
    }

    public long getSuccessorStabilizationPeriod() {
        return successorStabilizationPeriod;
    }

    public long getFingerStabilizationPeriod() {
        return fingerStabilizationPeriod;
    }

    public long getRpcTimeout() {
        return rpcTimeout;
    }

    public int getBootstrapRequestPeerCount() {
        return bootstrapRequestPeerCount;
    }

    public void store(String file) throws IOException {
        Properties p = new Properties();
        p.setProperty("log2.ring.size", "" + log2RingSize);
        p.setProperty("successor.list.length", "" + successorListLength);
        p.setProperty("successor.stabilization.period", ""
                + successorStabilizationPeriod);
        p.setProperty("finger.stabilization.period", ""
                + fingerStabilizationPeriod);
        p.setProperty("rpc.timeout", "" + rpcTimeout);
        p.setProperty("bootstrap.request.peer.count", ""
                + bootstrapRequestPeerCount);

        Writer writer = new FileWriter(file);
        p.store(writer, "se.sics.kompics.p2p.overlay.chord");
    }

    public static SONConfiguration load(String file) throws IOException {
        Properties p = new Properties();
        Reader reader = new FileReader(file);
        p.load(reader);

        int log2RingSize = Integer.parseInt(p.getProperty("log2.ring.size"));
        int successorListLength = Integer.parseInt(p
                .getProperty("successor.list.length"));
        long successorStabilizationPeriod = Long.parseLong(p
                .getProperty("successor.stabilization.period"));
        long fingerStabilizationPeriod = Long.parseLong(p
                .getProperty("finger.stabilization.period"));
        long rpcTimeout = Long.parseLong(p.getProperty("rpc.timeout"));
        int bootstrapRequestPeerCount = Integer.parseInt(p
                .getProperty("bootstrap.request.peer.count"));

        return new SONConfiguration(log2RingSize, successorListLength,
                successorStabilizationPeriod, fingerStabilizationPeriod,
                rpcTimeout, bootstrapRequestPeerCount);
    }
}